import { cacheTools } from 'nc-lightapp-front';
import CommonAction from './common';
import {addTempleteQueCon, COMMON, getBillid, getBilltype, updateButtonStatus} from "../main/util";

export default class DetailAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 获取详情页数据
    queryDetailData = async ({billid, isapprove, pk_org, callback} = {}) => {
        const {props} = this.comp;
        const {transfer, dispatch} = props;

        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !billid && (billid = transfer.billid);
        if (!billid) return;

        !isapprove && (isapprove = transfer.isapprove);
        !pk_org && (pk_org = transfer.hrorgobj.refpk);

        let postData = {
            billid,
            isapprove: !!isapprove,
            pk_org
        };
        try {
            let res = await dispatch({
                type: 'transfer/queryDetailData',
                payload: {
                    postData: postData
                }
            });
            if (!res.success) {
                return
            }
            dispatch({
                type: 'transfer/update',
                payload: {
                    billid
                }
            });
            // callback修改时，先渲染dom在输入数据
            if (callback && typeof callback === 'function') {
                callback();
            }
            if (res.data && res.data.formData) {
                let formId = COMMON.formId;
                this.setDetailData(res.data, formId, billid);
            }
        } catch (e) {

        }
    };

    setDetailData = async (data, formId, billid, status) => {

        const {props} = this.comp;
        const {dispatch, transfer} = props;
        let {formData, isBillCodeEditable, ...others} = data;
        if (!billid) {
            billid = getBillid(formData[formId].rows[0]);
        }

        //设置meta
        if(others){
            let template = props.meta.getMeta();
            Object.assign(template, others);
            await new Promise((resolve) => {
                addTempleteQueCon(template);
                props.meta.setMeta(template, resolve);
            });
        }
        let billtype = getBilltype(formData[formId].rows[0]);
        await dispatch({
            type: 'transfer/update',
            payload: {
                page: COMMON.pageDetail,
                billtype
            }
        });

        //单据号是否可编辑
        props.form.setFormItemsDisabled(formId, {bill_code: !data.isBillCodeEditable});
        // 我也是自闭了  偶现调配人disabled不对， 但是控制台打印出来 模板已经变化了 先这样手动处理
        props.form.setFormItemsDisabled(formId, {pk_psnjob: status === 'add_d' || status === 'add'? false: true})
        // 填写数据
        let formdata = {
            [formId]: {
                rows: formData[formId].rows
            }
        };
        props.form.setAllFormValue(formdata);
        if(transfer.isEditDetail){
            await props.form.setFormStatus(formId, 'edit');
        }
       this.setCardPage(billid);
    };

    setCardPage = async (billid)=> {
        const {props} = this.comp;
        const {dispatch} = props;
        // 更新按钮状态
        updateButtonStatus(props,billid);
        if(!billid) return;
        let allpks = cacheTools.get('allpks');
        if (!Array.isArray(allpks)) {
            allpks = [];
        }
        if(!allpks.includes(billid)){
            allpks.push(billid)
        }
        cacheTools.set('allpks', allpks);
        await dispatch({
            type:"transfer/update",
            payload:{
                billid
            }
        });
        props.cardPagination.setCardPaginationId({id: billid, status: 1});
    }

}